package threads;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestTaobaoThread2s_2 {
    private final static TestTaobaoThread2s_2 t = new TestTaobaoThread2s_2();
    private volatile List<String> list = Collections.synchronizedList(new ArrayList<>());

    public void add(String o) {
        list.add(o);
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (t) {
                try {
                    System.out.println("t2 wait");
                    t.wait();
                    t.notifyAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t2 end.");
        }, "t2").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                t.add(String.valueOf(i));
                System.out.println("add " + i);
                if (i == 5) {
                    synchronized (t){
                        try {
                            t.notifyAll();
                            t.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "t1").start();
    }
}
